package com.beyondsoft.common.core.sys.exception;

import com.beyondsoft.common.core.constant.CommonConstants;
import com.beyondsoft.common.core.constant.TransactionConstants;
import com.beyondsoft.common.core.exchange.sender.SubToMainSender;
import com.beyondsoft.common.core.redis.RedisMsService;
import com.beyondsoft.common.core.redis.RedisService;
import com.beyondsoft.common.core.sys.controller.CheckLoginController;
import com.beyondsoft.common.core.util.WebUtils;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 远程remote异常处理类
 */
@Aspect
@Component
public class RemoteHystrixException {

    @Autowired
    private RedisMsService redisMsService;

    @Pointcut("execution(public * com.beyondsoft.common.core.remote.hystrix.*Hystrix.*(..))")
    private void hystrixMethod() {
    }

    @Around("hystrixMethod()")
    public Object hystrixAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // start stopwatch   相当于是before advice
        //主线程
        String thread = Thread.currentThread().getName();
        //需要处理事务
        if (thread != null) {
            //redis key
            String packageKey = TransactionConstants.PACKAGEKEY + thread;
            //主线程直接失败
            redisMsService.set(packageKey, TransactionConstants.PACKAGEKEY_STATUS_ERROE);
        }

        Object retVal = proceedingJoinPoint.proceed();
        // stop stopwatch    相当于是after advice

        return retVal;
    }

}
